package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
	"encoding/json"
	"xiaoercaidan/publicResources/models"
	"github.com/goinggo/mapstructure"
	"fmt"
)

type UserController struct {
	beego.Controller
}

// @Title GetUserList
// @Description 用户列表
// @router /list [get]
func (this *UserController) GetUserList() {

	url := "http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/userList"

	fmt.Println(url)

	req := httplib.Get(url)

	resp, err := req.String()

	if (err == nil) {
		var mapStr map[string]interface{}

		json.Unmarshal([]byte(resp), &mapStr)

		users := make([]models.User, len(mapStr["data"].([]interface{})))

		for i := 0; i < len(mapStr["data"].([]interface{})); i++ {
			var user models.User
			user.Role = new(models.Role)

			mapstructure.Decode(mapStr["data"].([]interface{})[i], &user)

			user.Role.Status = mapStr["data"].([]interface{})[i].(map[string]interface{})["Role"].(map[string]interface{})["Status"].(string)
			user.Role.Remark = mapStr["data"].([]interface{})[i].(map[string]interface{})["Role"].(map[string]interface{})["Remark"].(string)

			user.Role.Id = int64(mapStr["data"].([]interface{})[i].(map[string]interface{})["Role"].(map[string]interface{})["Id"].(float64))

			user.Role.Name = mapStr["data"].([]interface{})[i].(map[string]interface{})["Role"].(map[string]interface{})["Name"].(string)
			user.Role.Title = mapStr["data"].([]interface{})[i].(map[string]interface{})["Role"].(map[string]interface{})["Title"].(string)
			users[i] = user
		}
		var BaseResult = new(models.BaseResult)

		BaseResult.Data = users

		this.Data["json"] = BaseResult

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = err.Error()
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()
}



// @Title UserUnique
// @Description 检测用户名唯一
// @router /userUnique [get]
func (this *UserController) UserUnique() {

	var name = this.GetString("name")

	var url = "http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/userUnique"
	fmt.Println(url)
	req := httplib.Get(url)
	req.Param("name", name)

	resp, err := req.String()

	if (err == nil) {
		var mapStr map[string]interface{}

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = &mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = err.Error()
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()
}




// @Title GetRoleList
// @Description 角色列表
// @router /roleList [get]
func (this *UserController) GetRoleList() {

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/roleList")

	resp, err := req.String()

	if (err == nil) {
		var mapStr map[string]interface{}

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr

	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
	}

	this.ServeJSON()

}


// @Title AddRole
// @Description 新增角色
// @router /roleAdd [get]
func (this *UserController) AddRole() {

	var role = this.GetString("role")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/roleAdd")
	req.Param("role", role)

	resp, err := req.String()

	if err != nil {
		this.Data["json"] = err.Error()
	} else {

		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)

		this.Data["json"] = mapStr
	}

	this.ServeJSON()

}




// @Title Login
// @Description 用户登录
// @router /login [get]
func (this *UserController) Login() {

	var userName = this.GetString("userName")
	var passWord = this.GetString("passWord")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/login?userName=" + userName + "&passWord=" + passWord)

	resp, err := req.String()

	if err == nil {

		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)

		if (mapStr["code"] != nil&& mapStr["code"].(float64) == 0 && mapStr["data"] != nil) {
			sess, _ := globalSessions.SessionStart(this.Ctx.ResponseWriter, this.Ctx.Request)
			sess.Set("User", mapStr["data"].(map[string]interface{}))
		}

		this.Data["json"] = mapStr
		this.ServeJSON()
	} else {

		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		this.Data["json"] = BaseResult
		this.ServeJSON()
	}

}


// @Title Regist
// @Description 会员注册
// @Param   user     patch string   true string       "用户json"
// @Failure 404 api not found
// @router /regist  [get]
func (c *UserController) Regist() {

	var userStr = c.GetString("user")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/regist")
	req.Param("user", userStr)

	resp, err := req.String()

	if err == nil {

		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)
		c.Data["json"] = mapStr
	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		c.Data["json"] = BaseResult

	}
	c.ServeJSON()

}


// @Title Del
// @Description 会员删除
// @Param   ids    patch string  true  string       "要删除的Id列表，用“,”分割"
// @Failure 404 api not found
// @router /del  [get]
func (c *UserController) Del() {

	var ids = c.GetString("ids")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/userDel")
	req.Param("ids", ids)

	resp, err := req.String()

	if err == nil {

		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)
		c.Data["json"] = mapStr
	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		c.Data["json"] = BaseResult

	}
	c.ServeJSON()

}



// @Title Update
// @Description 会员修改
// @Param   user    patch string  true  string       "用户json"
// @Failure 404 api not found
// @router /update  [get]
func (c *UserController) Update() {

	var user = c.GetString("user")

	req := httplib.Get("http://" + beego.AppConfig.String("basehost") + ":" + beego.AppConfig.String("baseport") + "/v1/apis/user/userUpdate")
	req.Param("user", user)

	resp, err := req.String()

	if err == nil {

		var mapStr = make(map[string]interface{})

		json.Unmarshal([]byte(resp), &mapStr)
		c.Data["json"] = mapStr
	} else {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "服务异常"
		c.Data["json"] = BaseResult

	}
	c.ServeJSON()

}









